cmake_minimum_required(VERSION 2.8) #проверка версии cmake

set(PROJECT storage) #присваиваем переменной PROJECT значение libscon

project(${PROJECT}) #задаем имя проекта

macro(add_app_executable BUILD_TYPE) #макрос задающий тип сборки проекта
	if(${BUILD_TYPE} STREQUAL "release")
		aux_source_directory("./sources" SOURCES)
		add_library(${PROJECT} SHARED ${SOURCES})
		set_target_properties(${PROJECT} PROPERTIES COMPILE_FLAGS "-O2 -std=c++11 -Wall")
		add_custom_command(TAGRET ${PROJECT} POST_BUILD
			COMMAND ${CMAKE_COMMAND} -E make_directory ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT}> ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E remove $<TARGET_FILE:${PROJECT}>
		)
	endif()

	if(${BUILD_TYPE} STREQUAL "debug")
		aux_source_directory("./sources" SOURCES)
		add_library(${PROJECT}_debug SHARED ${SOURCES})
		set_target_properties(${PROJECT}_debug PROPERTIES COMPILE_FLAGS "-O0 -g3 -std=c++11 -Wall")
		add_custom_command(TAGRET ${PROJECT}_debug POST_BUILD
			COMMAND ${CMAKE_COMMAND} -E make_directory ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT}> ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E remove $<TARGET_FILE:${PROJECT}>
		)
	endif()

	if(${BUILD_TYPE} STREQUAL "debug_test")
		include_directories("C:/gtest")
		link_directories("C:/gtest")
		aux_source_directory("./tests" SOURCES_TEST)
		aux_source_directory("./sources" SOURCES_TEST)
		add_executable(${PROJECT}_debug_test ${SOURCES_TEST})
		set_target_properties(${PROJECT}_debug_test PROPERTIES COMPILE_FLAGS "-O0 -g3 -std=c++11 -D__STORAGE_TEST__ -D__STORAGE_DEBUG__ -Wall")
		target_link_libraries(${PROJECT}_debug_test libgtest.a libgtest_main.a)
		run_test(${PROJECT}_debug_test)
		add_custom_command(TAGRET ${PROJECT}_debug_test POST_BUILD
			COMMAND ${CMAKE_COMMAND} -E make_directory ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT}_debug_test> ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E remove $<TARGET_FILE:${PROJECT}_debug_test>
		)
	endif()

	if(${BUILD_TYPE} STREQUAL "release_test")
		include_directories("C:/gtest")
		link_directories("C:/gtest")
		aux_source_directory("./tests" SOURCES_TEST)
		aux_source_directory("./sources" SOURCES_TEST)
		add_executable(${PROJECT}_debug_test ${SOURCES_TEST})
		set_target_properties(${PROJECT}_debug_test PROPERTIES COMPILE_FLAGS "-O2 -std=c++11 -D__STORAGE_TEST__ -Wall")
		target_link_libraries(${PROJECT}_debug_test libgtest.a libgtest_main.a)
		run_test(${PROJECT}_release_test)
		add_custom_command(TAGRET ${PROJECT}_release_test POST_BUILD
			COMMAND ${CMAKE_COMMAND} -E make_directory ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT}_release_test> ${BUILD_TYPE}
			COMMAND ${CMAKE_COMMAND} -E remove $<TARGET_FILE:${PROJECT}_release_test>
		)
	endif()

endmacro()

add_custom_target(test)
macro(run_test TEST_TARGET)
	add_custom_target(${TEST_TARGET}_runtest
		COMMAND ${TEST_TARGET}
		DEPENDS ${TEST_TARGET}
		WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
	add_dependencies(test ${TEST_TARGET}_runtest)
endmacro()

add_app_executable(debug_test)

#enable_testing()
